<?php

#    Document   : usercp
#    Created on : May 8, 2011, 12:34:32 AM
#    Author     : ClarkT AKA FalleN
#    Company    : Amodity
#    Web        : http://www.amodity.com
#    Copyright  : 2011
/**

 * @property CI_Loader $load

 * @property CI_Form_validation $form_validation

 * @property CI_Input $input

 * @property CI_Email $email

 * @property CI_DB_active_record $db

 * @property CI_DB_forge $dbforge

 *  @property CI_Session $session

 * @property CI_FTP $ftp

 */

class Usercp extends CI_Controller {

    function index() {
        //check if user is logged in
        $this->is_logged_in();

        //load pagination
        $this->load->library('pagination');
        $config['base_url'] = base_url() . 'index.php/usercp/index/';
        $config['total_rows'] = $this->db->get_where('images', array('author' => $this->session->userdata('username')))->num_rows();
        $config['per_page'] = 5;
        $config['num_links'] = 20;

        $this->pagination->initialize($config);

        $query = $this->db->get_where('images', array('author' => $this->session->userdata('username')), $config['per_page'], $this->uri->segment(3));
        $row = $query->result_array();

        //load config
        $this->config->load('upload');

        //get total images
        $total = $this->db->get_where('images', array('author' => $this->session->userdata('username')))->num_rows();


        //order data for output
        $data = array(
            'title' => $this->lang->line('cp_cp'),
            'main_content' => 'usercp/dashboard',
            'row' => $row,
            'query' => $this->db->get_where('images', array('author' => $this->session->userdata('username')), $config['per_page'], $this->uri->segment(3)),
            'upload_path' => substr($this->config->item('upload_path'), 2),
            'pagination' => $this->pagination->create_links(),
            'total' => $total
        );
        $this->template->set('title', lang('cp_cp'));
        $this->template->load('site_template', 'usercp/dashboard', $data);
    }

    function is_logged_in() {
        $this->load->library('auth');
        if (!$this->auth->logged_in()) {
            redirect('login');
        }
    }

    function userimages($status = null) {

        //check if user is logged in
        $this->is_logged_in();

        $this->load->model('usercp_model');
        $row = $this->usercp_model->getusers();

        //load config
        $this->config->load('upload');

        //get total images
        $total = $this->usercp_model->total();

        //order data for output
        $data = array(
            'row' => $row,
            'upload_path' => substr($this->config->item('upload_path'), 2),
            'pagination' => $this->pagination->create_links(),
            'total' => $total,
            'status' => $status
        );
        $this->template->set('title', lang('cp_your_images'));
        $this->template->load('site_template', 'usercp/user_images', $data);
    }

    function delete($imagename = null) {
        //is user logged in
        $this->is_logged_in();

        //load Model and get images by user
        $this->load->model('Usercp_model');
        $row = $this->Usercp_model->userimages($this->session->userdata('username'));

        //remove image and db record
        $this->db->where('name', $imagename);
        $this->db->delete('images');
        //load config
        $this->config->load('upload');

        $path = substr($this->config->item('upload_path'), 0) . $imagename;
        $remove = unlink($path);

        if ($remove) {
            $this->message->set('success', $this->lang->line('cp_file_removed'));
        } else {
            $this->message->set('error', $this->lang->line('cp_file_remove_error'));
        }
        $this->userimages($status);
    }

    function editprofile() {
        //is user logged in
        $this->is_logged_in();

        //Do edits
        if ($this->input->post('doedit')) {
            //do user edits in database
            $data = array(
                'first_name' => $this->input->post('firstname'),
                'last_name' => $this->input->post('lastname'),
                'email_address' => $this->input->post('email')
            );
            $this->db->where('username', $this->session->userdata('username'));
            $query = $this->db->update('users', $data);
            if ($query) {
                $this->session->set_flashdata(array('success',  $this->lang->line('cp_useredit_success')));
            } else {
                $this->session->set_flashdata(array('error',  $this->lang->line('cp_useredit_failed')));
            }
            //get user info from db
            $query = $this->db->get_where('users', array('username' => $this->session->userdata('username')));
            foreach ($query->result() as $row) {
                
            }
        }

        //get user info from db
        $query = $this->db->get_where('users', array('username' => $this->session->userdata('username')));
        foreach ($query->result() as $row) {
            
        }

        //prepare data for output
        $data = array(
            'row' => $row,
            'status' => $this->session->flashdata('success')
        );
        $this->template->set('title', lang('cp_edit_profile'));
        $this->template->load('site_template', 'usercp/editprofile', $data);
    }

    function do_edit() {
        //do user edits in database
        $data = array(
            'first_name' => $this->input->post('firstname'),
            'last_name' => $this->input->post('lastname'),
            'email_address' => $this->input->post('email')
        );
        $this->db->where('username', $this->session->userdata('username'));
        $query = $this->db->update('users', $data);
        if ($query) {
            $data['status'] = $this->lang->line('cp_useredit_success');
        } else {
            $data['status'] = $this->lang->line('cp_useredit_failed');
        }
        //get user info from db
        $query = $this->db->get_where('users', array('username' => $this->session->userdata('username')));
        foreach ($query->result() as $row) {
            
        }
        $data['row'] = $row;
        $this->template->set('title', lang('cp_edit_profile'));
        $this->template->load('site_template', 'usercp/editprofile', $data);
    }

    function logout() {
        $sesdata = array('username' => '', 'is_logged_in' => '', 'access_level' => '');
        $this->session->unset_userdata($sesdata);
        $this->message->set('success', lang('cp_logout'));
        $this->template->set('title', lang('g_login'));
        $this->template->set('bodyid', 'login');
        $this->template->load('site/template', 'login/login_form');
    }

}

?>
